package org.example.jdk8.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class test7 {
    public static void main(String[] args) {
        Student student1 =new Student("zhangsan",100,20);
        Student student2 =new Student("lisi",90,20);
        Student student3 =new Student("wangwu",90,30);
        Student student4=new Student("zhangsan",80,40);
        List<Student> students = Arrays.asList(student1,student2,student3,student4);
        // 按照姓名分组
        /*Map<String, List<Student>> sMapGroup = students.stream().collect(Collectors.groupingBy(Student::getName));
        sMapGroup.forEach((key,value)->System.out.println(key+" " + value));*/

        // 按照姓名分组,并过滤分数小于90的
        /*students.stream().filter(s -> s.getScore() >= 90)
                .collect(Collectors.groupingBy(Student::getName))
                .forEach((key,value)->System.out.println(key+" " + value));*/

        //返回每个分组成员个数
        /*Map<String, Long> sCount = students.stream().collect(Collectors.groupingBy(Student::getName, Collectors.counting()));
        sCount.forEach((key,value)->System.out.println(key+" " + value));*/

        //按照姓名分组返回每个分组成员的平均年龄
        /*Map<String, Double> sAvg = students.stream().collect(Collectors.groupingBy(Student::getName, Collectors.averagingInt(Student::getAge)));
        sAvg.forEach((key,value)->System.out.println(key+" " + value));*/

        //按照大于等于90的分数分区
        Map<Boolean, List<Student>> sPartition = students.stream().collect(Collectors.partitioningBy(s -> s.getScore() >= 90));
        sPartition.forEach((k,v)->System.out.println(k+ ":" +v));

    }
}
class Student{

    public Student(String name, int score, int age) {
        this.name = name;
        this.score = score;
        this.age = age;
    }

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private int score;
    private int age;
}
